
//
//  RootViewController.swift
//  LoveLife
//
//  Created by 杨阳 on 16/9/6.
//  Copyright © 2016年 yangyang. All rights reserved.
//

import UIKit

class RootViewController: UIViewController {

    //左按钮
    var leftButton:UIButton?
    //标题
    var titleLabel:UILabel?
    //右按钮
    var rightButton:UIButton?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置背景颜色
        self.view.backgroundColor = UIColor.whiteColor()
        
        //设置导航的颜色
        self.navigationController?.navigationBar.barTintColor = UIColor.RGBA(251, 170, 124)
        //设置导航不透明
        self.navigationController?.navigationBar.translucent = false
        
        //方式一：只适用于控制器中修改，如果要全局进行修改的话，所有的控制器都必须继承自同一个父类
        //修改状态栏的颜色,默认为黑色,UIBarStyle.Default表示黑色，UIBarStyle.Black表示就是白色，是一个相对值
        //self.navigationController?.navigationBar.barStyle = UIBarStyle.Black
        
        
        //左按钮
        self.leftButton = UIButton(type: UIButtonType.Custom)
        self.leftButton?.frame = CGRectMake(0, 0, 44, 44)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: self.leftButton!)
        
        //标题
        self.titleLabel = FactoryUI.createLabel(CGRectMake(0, 0, 100, 44), text: nil, textColor: UIColor.whiteColor(), font: UIFont.boldSystemFontOfSize(20), textAligment: NSTextAlignment.Center)
        self.navigationItem.titleView = self.titleLabel
        
        //右按钮
        self.rightButton = UIButton(type: UIButtonType.Custom)
        self.rightButton?.frame = CGRectMake(0, 0, 44, 44)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: self.rightButton!)
        
            
    }
    
    //定义实例方法--按钮的响应方法
    //左按钮
    func addLeftButtonAction(selector:Selector) {
        self.leftButton?.addTarget(self, action: selector, forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    //右按钮
    func addRightButtonAction(selector:Selector) {
        self.rightButton?.addTarget(self, action: selector, forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    
    //显示警告框
    func showAlert(message:String,completion:(()->())?) {
        
        let alertController = UIAlertController.init(title: nil, message: message, preferredStyle: .Alert)
        
        let action = UIAlertAction.init(title: "确定", style: .Default) { (_) in
            
            if completion != nil{
                
                completion!()
            }
        }
        alertController.addAction(action)
        self.presentViewController(alertController, animated: true, completion: nil)
//        //多长时间
//        let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC)))
//        dispatch_after(dispatchTime, dispatch_get_main_queue()) { () -> Void in
//            alertController.dismissViewControllerAnimated(true, completion: nil)
        
        }
    

    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
